package com.xtravel.common.helper;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Toast;
import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;

import cn.sharesdk.sina.weibo.SinaWeibo;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.tencent.qzone.QZone;
import cn.sharesdk.wechat.friends.Wechat;
import cn.sharesdk.wechat.moments.WechatMoments;
import com.xtravel.common.share.OnekeyShare;
import com.xtravel.common.share.ShareContentCustomizeCallback;
import com.xtravel.common.widget.ShareDialog;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by memory on 15-8-15.
 */
public final class ShareSdkHelper {

    private static Context context;

    public static void share(Context context, final String title, final String content, final String contentUrl, final ArrayList<String> urls) {
        OnekeyShare oks = new OnekeyShare();
        //不显示编辑界面
        //oks.setSilent(true);
        oks.setTitle(title);
        oks.setText(content);
        //titleUrl是标题的网络链接,仅在人人网和QQ空间使用
        oks.setTitleUrl(contentUrl);
        //siteUrl是分享此内容的网站地址,仅在QQ空间使用
        oks.setSiteUrl(contentUrl);
        //url仅在微信（包括好友和朋友圈）中使用
        oks.setUrl(contentUrl);
        if (urls != null && urls.size() > 0) {
            oks.setImageArray(urls.toArray(new String[]{}));
            oks.setImageUrl(urls.get(0));
            //本地路径
            //oks.setImagePath(urls.get(0));
        }

        //自定义平台分享内容
        oks.setShareContentCustomizeCallback(new ShareContentCustomizeCallback() {
            @Override
            public void onShare(Platform platform, Platform.ShareParams paramsToShare) {
                if (SinaWeibo.NAME.equals(platform.getName())) {
                    String titleUrl = paramsToShare.getTitleUrl() + " ";
                    String content = paramsToShare.getText();
                    Pattern pattern = Pattern.compile("[a-zA-z]+://[^\\s].+?[^\u4e00-\u9fa5\\s]*");
                    Matcher matcher = pattern.matcher(content);
                    while (matcher.find()) {
                        String r = matcher.group();
                        content = content.replace(r, titleUrl);
                    }

                    paramsToShare.setText(content);
                }
            }
        });

        oks.show(context);
    }

    public static void shareLocalFile(Context context, String content, String localFile) {
        ShareSDK.initSDK(context);
        OnekeyShare oks = new OnekeyShare();
        oks.setSilent(false);
        oks.setText(content);
        oks.setTitle("分享出行清单");
        oks.setTitleUrl("http://www.xtravels.net");
        oks.setSiteUrl("http://www.xtravels.net");
        oks.setUrl("http://www.xtravels.net");
        if (localFile != null && !localFile.isEmpty()) {
            String[] urls = new String[1];
            urls[0] = localFile;
            oks.setImageArray(urls);
            oks.setImagePath(localFile);
        }
        oks.show(context);
    }

    public static void customShare(Context context, final String content, final ArrayList<String> urls) {
        ShareSdkHelper.context = context;
        ShareDialog dialog = new ShareDialog(context);
        dialog.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                HashMap<String, Object> item = (HashMap<String, Object>) adapterView.getItemAtPosition(i);
                String platform = item.get("ItemText").toString();
                ShareSdkHelper.innerShare(platform, content, urls);
            }
        });
    }

    private static void innerShare(String platfrom, String content, ArrayList<String> urls) {
        switch (platfrom) {
            case "QQ":
                shareToQQ(content, urls.get(0));
                break;
            case "QQ空间":
                shareToQZone(content, urls.get(0));
                break;
            case "微博":
                shareToSina(content, urls.get(0));
                break;
            case "微信":
                shareToWeChat(content, urls.get(0));
                break;
            case "朋友圈":
                shareToWeChatFriend(content, urls.get(0));
                break;
            default:
                break;
        }
    }

    private static void shareToQQ(String content, String url) {
        QQ.ShareParams sp = new QQ.ShareParams();
        sp.setTitle("我是分享标题");
        sp.setText(content);
        sp.setImageUrl(url);//网络图片rul
        sp.setTitleUrl("http://www.baidu.com");
        //3、非常重要：获取平台对象
        Platform qq = ShareSDK.getPlatform(QQ.NAME);
        qq.setPlatformActionListener(actionListener);
        // 执行分享
        qq.share(sp);
    }

    private static void shareToQZone(String content, String url) {
        QZone.ShareParams sp = new QZone.ShareParams();
        sp.setTitle("我是分享标题");
        sp.setText(content);
        sp.setImageUrl(url);//网络图片rul
        sp.setTitleUrl("http://www.baidu.com");//网友点进链接后，可以看到分享的详情
        //3、非常重要：获取平台对象
        Platform qzone = ShareSDK.getPlatform(QZone.NAME);
        qzone.setPlatformActionListener(actionListener);
        // 执行分享
        qzone.share(sp);
    }

    private static void shareToSina(String content, String url) {
        SinaWeibo.ShareParams sp = new SinaWeibo.ShareParams();
        sp.setText(content); //分享文本
        sp.setImageUrl(url);//网络图片rul
        Platform sinaWeibo = ShareSDK.getPlatform(SinaWeibo.NAME);
        sinaWeibo.setPlatformActionListener(actionListener);
        // 执行分享
        sinaWeibo.share(sp);
    }

    private static void shareToWeChat(String content, String url) {
        Wechat.ShareParams sp = new Wechat.ShareParams();
        sp.setShareType(Platform.SHARE_WEBPAGE);//非常重要：一定要设置分享属性
        sp.setTitle("我是分享标题");  //分享标题
        sp.setText(content);   //分享文本
        sp.setImageUrl(url);//网络图片rul
        //sp.setUrl("http://sharesdk.cn");   //网友点进链接后，可以看到分享的详情

        Platform wechat = ShareSDK.getPlatform(Wechat.NAME);
        wechat.setPlatformActionListener(actionListener);
        // 执行分享
        wechat.share(sp);
    }

    private static void shareToWeChatFriend(String content, String url) {
        WechatMoments.ShareParams sp = new WechatMoments.ShareParams();
        sp.setShareType(Platform.SHARE_WEBPAGE); //非常重要：一定要设置分享属性
        sp.setTitle("我是分享标题");  //分享标题
        sp.setText(content);   //分享文本
        sp.setImageUrl(url);//网络图片rul
        sp.setUrl("http://www.baidu.com");//网友点进链接后，可以看到分享的详情

        Platform wechatMoments = ShareSDK.getPlatform(WechatMoments.NAME);
        wechatMoments.setPlatformActionListener(actionListener);
        // 执行分享
        wechatMoments.share(sp);
    }

    private static PlatformActionListener actionListener = new PlatformActionListener() {
        @Override
        public void onCancel(Platform arg0, int arg1) {//回调的地方是子线程，进行UI操作要用handle处理
            handler.sendEmptyMessage(7);
        }

        @Override
        public void onComplete(Platform arg0, int arg1, HashMap<String, Object> arg2) {//回调的地方是子线程，进行UI操作要用handle处理
            if (arg0.getName().equals(SinaWeibo.NAME)) {// 判断成功的平台是不是新浪微博
                handler.sendEmptyMessage(1);
            } else if (arg0.getName().equals(Wechat.NAME)) {
                handler.sendEmptyMessage(1);
            } else if (arg0.getName().equals(WechatMoments.NAME)) {
                handler.sendEmptyMessage(3);
            } else if (arg0.getName().equals(QQ.NAME)) {
                handler.sendEmptyMessage(4);
            } else if (arg0.getName().equals(QZone.NAME)) {
                handler.sendEmptyMessage(5);
            }
        }

        @Override
        public void onError(Platform arg0, int arg1, Throwable arg2) {//回调的地方是子线程，进行UI操作要用handle处理
            arg2.printStackTrace();
            Message msg = new Message();
            msg.what = 6;
            msg.obj = arg2.getMessage();
            handler.sendMessage(msg);
        }
    };

    private static Handler handler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    Toast.makeText(context, "微博分享成功", Toast.LENGTH_LONG).show();
                    break;
                case 2:
                    Toast.makeText(context, "微信分享成功", Toast.LENGTH_LONG).show();
                    break;
                case 3:
                    Toast.makeText(context, "朋友圈分享成功", Toast.LENGTH_LONG).show();
                    break;
                case 4:
                    Toast.makeText(context, "QQ分享成功", Toast.LENGTH_LONG).show();
                    break;
                case 5:
                    Toast.makeText(context, "QQ空间分享成功", Toast.LENGTH_LONG).show();
                    break;
                case 6:
                    Toast.makeText(context, "分享失败啊" + msg.obj, Toast.LENGTH_LONG).show();
                    break;
                case 7:
                    Toast.makeText(context, "取消分享", Toast.LENGTH_LONG).show();
                    break;
                default:
                    break;
            }
        }
    };
}
